package com.lframework.xingyun.basedata.bo.make;

import cn.hutool.core.util.StrUtil;
import com.lframework.starter.web.core.bo.BaseBo;
import com.lframework.starter.web.core.utils.ApplicationUtil;
import com.lframework.xingyun.basedata.dto.make.MakeMethodMaterialDto;
import com.lframework.xingyun.basedata.entity.MakeMethod;
import com.lframework.xingyun.basedata.service.make.MakeMethodMaterialService;
import io.swagger.annotations.ApiModelProperty;

import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 制作方法表 GetBo
 * </p>
 *
 */
@Data
public class GetMakeMethodBo extends BaseBo<MakeMethod> {

    /**
     * ID
     */
    @ApiModelProperty("ID")
    private String id;

    /**
     * 制作名称
     */
    @ApiModelProperty("制作名称")
    private String name;

    /**
     * 制作描述
     */
    @ApiModelProperty("制作描述")
    private String description;

    /**
     * 状态
     */
    @ApiModelProperty("状态")
    private Boolean available;

    /**
     * 材料列表
     */
    @ApiModelProperty("材料列表")
    private List<MakeMethodMaterialBo> materials;

    public GetMakeMethodBo() {

    }

    public GetMakeMethodBo(MakeMethod dto) {

        super(dto);
    }

    @Override
    public BaseBo<MakeMethod> convert(MakeMethod dto) {
        return super.convert(dto);
    }

    @Override
    protected void afterInit(MakeMethod dto) {
        MakeMethodMaterialService makeMethodMaterialService = ApplicationUtil.getBean(MakeMethodMaterialService.class);
        List<MakeMethodMaterialDto> makeMethodMaterialDtos = makeMethodMaterialService.selectDtoByMakeId(dto.getId());
        Map<String, List<MakeMethodMaterialBo>> replacementGroup = makeMethodMaterialDtos.stream()
                .filter(item -> item.getType() == 1)
                .map(MakeMethodMaterialBo::new)
                .collect(Collectors.groupingBy(item -> StrUtil.format("{}/{}", item.getMainCode(), item.getMainSkuCode())));
        this.materials = makeMethodMaterialDtos.stream()
                .filter(item -> item.getType() == 0)
                .map(item -> {
                    MakeMethodMaterialBo  makeMethodMaterialBo = new MakeMethodMaterialBo(item);
                    makeMethodMaterialBo.setReplacements(replacementGroup.get(StrUtil.format("{}/{}", item.getCode(), item.getSkuCode())));
                    return makeMethodMaterialBo;
                }).collect(Collectors.toList());
    }
}

